-- @Author:      shine
-- @Description: 消息的处理

local skynet = require "skynet"
local constant = require("constant")
local reason = require("reason")
local cjson = require("cjson")
local mail_def = require("mail_def")
local table_insert = table.insert
local table_remove = table.remove
local math_random = math.random
local math_floor = math.floor


local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch)
	self.message_dispatch = message_dispatch	

	self:register()
end

--注册本服务里的消息
function MessageHandler:register()
	--启动
	self.message_dispatch:registerSelf('start', handler(self,self.start))
	self.message_dispatch:registerSelf('stop', handler(self,self.stop))

    self.message_dispatch:registerSelf('on_rescue_win_lose', handler(self,self.onRescueWinLose)) 
    self.message_dispatch:registerSelf('get_rescue_conf', handler(self,self.getRescueConf)) 
    self.message_dispatch:registerSelf('get_state', handler(self,self.getActState)) 
end


function MessageHandler:createUser(uid)
    self.config:reload()
    local user = self.user_manager:addUser(uid) 
    local act_data = self.config:getActivity() --活动配置
    local ac_content = act_data.ac_content or {}       
    local counts = ac_content.pack_num or {1, 10}
    local max_count = math_random(counts[1], counts[2])
    user:setMaxRedpackNum(max_count)    
    return user   
end

------------------------------------------------------------------
--cmd
------------------------------------------------------------------
function MessageHandler:start(act_id)
	print("______rescue_start_______")
    self.act_id = act_id
    self.config = global.config 
    self.user_manager = global.user_manager
    self.desk = global.desk
    self.config:start(act_id)
    self.desk:start()

    self.act_data = self.config:getActivity() --活动配置
    self.ac_content = self.act_data.ac_content
end

function MessageHandler:stop()
    self.desk:stopTimer()
    skynet.sleep(10)   
    dbglog("_______活动关闭________",self.act_id)
    skynet.exit()
end


function MessageHandler:onRescueWinLose(msg)
    local time = msg.time
    self.act_data = self.config:getActivity() --活动配置
    -- --开放的时间段
    -- self.begin_time = string_time(self.act_data.ac_begin_time)
    -- self.end_time = string_time(self.act_data.ac_end_time)

    -- if time < self.begin_time or time > self.end_time then
    --     return
    -- end 

    local uid = msg.uid        
    -- local user = self.user_manager:getUser(uid)
    -- if not user then         
    --     user = self:createUser(uid)
    --     if not user then 
    --         errlog("_____用户不存在__",uid)
    --         return 
    --     end     
    -- end 

    dbglog("____onRescueWinLose_____", msg)

    --计算救援金
    local coins = 0
    local coins_range_conf = self.act_data.ac_content.coins_range or {}
    local vip_level = msg.vip_level or 0
    --配置中1金币=100分
    for i = #coins_range_conf , 1 , -1 do
        local value = coins_range_conf[i]
        if value.param <= msg.water then
            local vip_value = value.coins_range or {}
            coins = vip_value[vip_level + 1] or 0
            dbglog("___onRescueWinLose__vip___", vip_level, vip_value, coins, msg)
            break
        end
    end

    if coins == 0 then
        -- dbglog(coins_range_conf)
        dbglog("___onRescueWinLose___coins__", uid, coins)
        return 
    end
    
    -- self.desk:addChip(user, coins, reason.RESCUE)
    --发送邮件
    local mail_list = {}
    local mail = {}
    mail.uid = uid
    mail.title = string.format(mail_def.RESCUE_TITLE)
    mail.content = string.format(mail_def.RESCUE_CONTENT, coins/100)
    mail.coins = coins
    mail.mail_type = 2
    mail.op_user = mail_def.OP_USER
    mail.reason = reason.RESCUE
    table_insert(mail_list,mail)

    print("send_rescue mial =========",tostring_r(mail_list))
    self.desk:sendAwardMail(mail_list)
    -- self.user_manager:removeUser(uid)

    self.desk:opPartakeNum(uid, constant.DODULE_ID.DAY_ALMS)
    return true
end

function MessageHandler:getRescueConf(msg)
    local uid = msg.uid
    if not uid then
        return
    end    
    local user = self.user_manager:addUser(uid) 
    if not user then 
        errlog("_____用户不存在__",uid)
        return 
    end

    local vip_level = user:getVipLevel()
    local conf_item = {}
    dbglog("rescue",vip_level,self.act_data)
    local coins_range_conf = self.act_data.ac_content.coins_range or {}
    for _,v in  pairs(coins_range_conf) do
       table_insert(conf_item,{param = v.param ,coins = v.coins_range[vip_level + 1]}) 
    end
    self.user_manager:removeUser(uid)
   
    dbglog("_____getRescueConf___res____", conf_item)
    return {conf_item = conf_item,}    
end

function MessageHandler:getActState()
    local res = {
        rescue = {
            gain_count = 0,
            max_count = 0,
            coins = 0,
            all_coins = 0,
            countdown = 0,
            is_open = 1,  

        }
    }    
    dbglog("_____getActState___res____", res)
    return res    
end

return MessageHandler